package sc.prototype.pfs.eventnotify.collector.impl;

import java.util.HashMap;
import java.util.List;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sc.prototype.pfs.eventnotify.Configs;
import sc.prototype.pfs.eventnotify.api.UniNotice;
import sc.prototype.pfs.eventnotify.collector.ContentCollector;
import sc.prototype.pfs.eventnotify.collector.ContentCollectorFactory;
import sc.prototype.pfs.eventnotify.collector.NoticeFactory;
import sc.prototype.pfs.eventnotify.excep.CollectContentException;
import sc.prototype.pfs.eventnotify.model.Notice;
import sc.prototype.pfs.eventnotify.model.NoticeContent;
import sc.prototype.pfs.eventnotify.model.NotifyConfig;
import sc.prototype.pfs.eventnotify.model.NotifyContext;
import sc.prototype.pfs.eventnotify.model.NotifyTarget;
import sc.prototype.pfs.eventnotify.model.enums.NotifyStatus;

public class DefaultNoticeFactory implements NoticeFactory {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    ContentCollectorFactory contentCollectorFactory;

    @Override
    public CreateStuff create(NotifyConfig config, NotifyContext context) throws CollectContentException {
        CreateStuff stuff = new CreateStuff();
        List<NoticeContent> noticeContents = stuff.getNoticeContents();
        List<Notice> notices = stuff.getNotices();
        NoticeContent noticeContent = new NoticeContent();
        noticeContents.add(noticeContent);
        //采集内容
        HashMap<String, String> noticeExts = context.getEvent().getExtensions();
        ContentCollector contentCollector = contentCollectorFactory.create(config, context);
        UniNotice content = contentCollector.collect(config, context);
        noticeContent.setContent(content);
        noticeContent.setContentType(content.getNoticeType());
        noticeContent.setContentKey(context.getEvent().getEventKey());
        for (NotifyTarget notifyTarget : config.getNotifyTargets()) {
            Notice notice = new Notice();
            notice.setNotifyTarget(notifyTarget);
            notice.setContent(noticeContent);
            notice.setGmtAllowNotify(new DateTime().plusSeconds(Configs.INS.RETRY_LOCK_SECONDS).toDate());
            notice.setNotifyConfig(config);
            notice.setNotifyCount(0);
            notice.setNotifyStatus(NotifyStatus.INIT);
            notice.setOperator(Configs.INS.NOTICE_OPERATOR);
            notice.setExtensions(noticeExts);
            notices.add(notice);
        }
        return stuff;
    }



    public void setContentCollectorFactory(ContentCollectorFactory contentCollectorFactory) {
        this.contentCollectorFactory = contentCollectorFactory;
    }

}
